package com.hunt.controller;


import com.hunt.dao.TransactionCalendarMapper;
import com.hunt.model.entity.NorthBoundStockClassify;
import com.hunt.model.entity.NorthBoundStocks;
import com.hunt.model.entity.NorthCrawler;
import com.hunt.model.entity.NorthCapitalHoldingsRank;
import com.hunt.service.AppNorthCrawlerService;
import com.hunt.util.Result;
import com.hunt.util.ResultForNorth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author sjs
 * @Date 2021/1/15
 * @Description
 */
@Api(value = "手机端北上资金模块")
@Controller
@RequestMapping("northFund")
public class AppNorthCrawlerController extends BaseController{

    @Autowired
    private AppNorthCrawlerService appNorthCrawlerService;
    @Autowired
    private TransactionCalendarMapper transactionCalendarMapper;
    private  static  Integer count= 0;

    @ApiOperation(value = "获取全部北上资金记录", httpMethod = "GET", produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getAllNorth", method = RequestMethod.GET)
    public Result getAllNorth() {
        List<NorthCrawler> northCrawlerList = appNorthCrawlerService.getAllNorthCrawler();
        if(northCrawlerList.size()>0){
            return ResultForNorth.success(northCrawlerList);
        }else {
            return ResultForNorth.empty(northCrawlerList);
        }
    }

    @ApiOperation(value = "北上资金个股排行(此方法不会触发过滤器token校验)", httpMethod = "GET", produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getAllNorthBoundStocks", method = RequestMethod.GET)
    public Result getAllNorthBoundStocks(@RequestParam String time,@RequestParam String type, @RequestParam(required = false,defaultValue = "0") Integer position, @RequestParam(required = false,defaultValue = "10") Integer size) {
        switch(time){
            case "1" :
                return Result.success(appNorthCrawlerService.getNorthStocks(type,position,size));
            case "3" :
                return Result.success(appNorthCrawlerService.getNorthStocksThree(type,position,size));
            case "5" :
                return Result.success(appNorthCrawlerService.getNorthStocksFive(type,position,size));
            case "10" :
                return Result.success(appNorthCrawlerService.getNorthStocksTen(type,position,size));
            case "30" :
                return Result.success(appNorthCrawlerService.getNorthStocksMonth(type,position,size));
        }
        return  Result.success();
    }

    @ApiOperation(value = "app_北上资金个股排行新(此方法会触发过滤器token校验)", httpMethod = "GET", produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "app_getAllNorthBoundStocksnew", method = RequestMethod.GET)
    public Result app_getAllNorthBoundStocksnew(@RequestParam String time,@RequestParam String type, @RequestParam(required = false,defaultValue = "0") Integer position, @RequestParam(required = false,defaultValue = "10") Integer size) {
        switch(time){
            case "1" :
                return Result.success(appNorthCrawlerService.getNorthStocks(type,position,size));
            case "3" :
                return Result.success(appNorthCrawlerService.getNorthStocksThree(type,position,size));
            case "5" :
                return Result.success(appNorthCrawlerService.getNorthStocksFive(type,position,size));
            case "10" :
                return Result.success(appNorthCrawlerService.getNorthStocksTen(type,position,size));
            case "30" :
                return Result.success(appNorthCrawlerService.getNorthStocksMonth(type,position,size));
        }
        return  Result.success();
    }
    @ApiOperation(value = "app_北上资金个股排行(此方法会触发过滤器token校验)", httpMethod = "GET", produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "app_getAllNorthBoundStocks", method = RequestMethod.GET)
    public Result app_getAllNorthBoundStocks(@RequestParam String type, @RequestParam(required = false,defaultValue = "0") Integer position, @RequestParam(required = false,defaultValue = "10") Integer size) {
                return Result.success(appNorthCrawlerService.getNorthStocks(type,position,size));
    }
    @ApiOperation(value = "北向资金增持行业板块排行", httpMethod = "GET", produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getNorthRank", method = RequestMethod.GET)
    public Result getNorthRank(@RequestParam String time,
                                         @RequestParam String rankId,
                                         @RequestParam(required = false) Integer position,
                                         @RequestParam(required = false) Integer size) {
        //北向资金增持行业板块排行-今日
        if(time.equals("1")) {
            List<NorthCapitalHoldingsRank> northStocks = appNorthCrawlerService.getNorthHoldingsRank(rankId,position,size);
            return Result.success(northStocks);
            //北向资金增持行业板块排行-3日
        } else if(time.equals("3")){
            List<NorthCapitalHoldingsRank> northStocks = appNorthCrawlerService.getNorthRank3day(rankId,position,size);
            return Result.success(northStocks);
        }
        return Result.success();
    }
    @ApiOperation(value = "北向资金筛选结果查询", httpMethod = "GET", produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getNorthScreeningResult", method = RequestMethod.GET)
    public Result getNorthScreeningResult(@RequestParam @ApiParam(value = "0为仓位新高,1为连续增仓,2为逆势买入")Integer type, @RequestParam(value = "date",defaultValue = "0") String date, @RequestParam(value = "orederfield",defaultValue = "shareholddate",required = false) String orederfield
     ,@RequestParam(value = "sort",defaultValue = "0",required = false) String sort) {
        //0代表最新的
        if ("0".equals(date)&&type == 0) {
            return Result.success(appNorthCrawlerService.getNorthHigestLatest(date,orederfield,sort));
        }
        if ("0".equals(date)&&type == 1) {
            return Result.success(appNorthCrawlerService.getNorthStillAddLatest(date,orederfield,sort));
        }
        if ("0".equals(date)&&type == 2) {
            return Result.success(appNorthCrawlerService.getNorthReverseBuyLatest(date,orederfield,sort));
        }
        if (type == 0) {
            return Result.success(appNorthCrawlerService.getNorthHigest(date,orederfield,sort));
        }
        if (type == 1) {
            return Result.success(appNorthCrawlerService.getNorthStillAdd(date,orederfield,sort));
        }
        if (type == 2) {
            return Result.success(appNorthCrawlerService.getNorthReverseBuy(date,orederfield,sort));
        }
        return null;
    }
    @ApiOperation(value = "北向资金历史结果查询", httpMethod = "GET", produces = "application/json;charset=utf-8", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getNorthHistoryResult", method = RequestMethod.GET)
    public Result getNorthHistoryResult(@RequestParam(value = "code",defaultValue = "0") String code) {
        return Result.success(appNorthCrawlerService.getNorthHistory(code));
    }
//    private List<NorthBoundStockClassify> getnbs(Integer type,String date){
//        if(count>10){
//            List<NorthBoundStockClassify> nbs=new ArrayList<>();
//            count=0;
//            return nbs;
//        }
//        if (type == 0) {
//            List<NorthBoundStockClassify> nbs=appNorthCrawlerService.getNorthHigest(date);
//            if(nbs.size()<1){
//                date=transactionCalendarMapper.getbefor(date);
//                count++;
//                getnbs(type,date);
//
//            }else {
//                count=0;
//              return nbs;
//            }
//        }
//        if (type == 1) {
//            List<NorthBoundStockClassify> nbs=appNorthCrawlerService.getNorthStillAdd(date);
//            if(nbs.size()<1){
//                date=transactionCalendarMapper.getbefor(date);
//                count++;
//                getnbs(type,date);
//            }else {
//                count=0;
//                return nbs;
//            }
//        }
//        if (type == 2) {
//            List<NorthBoundStockClassify> nbs=appNorthCrawlerService.getNorthReverseBuy(date);
//            if(nbs.size()<1){
//                date=transactionCalendarMapper.getbefor(date);
//                count++;
//                getnbs(type,date);
//            }else {
//                count=0;
//                return nbs;
//            }
//        }
//        System.out.println(type);
//        List<NorthBoundStockClassify> nbs=new ArrayList<>();
//        return nbs;
//    }
}
